<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

/**
 * 对用户的密码进行加密
 * @param $password
 * @param $encrypt //传入加密串，在修改密码时做认证
 * @return array/password
 */
function encrypt_password($password)
{
    $pass = md5(md5($password));
    return $pass;
}
function json_x($type = '-1', $msg = '失败')
{
    return json(['code' => $type, 'msg' => $msg]);
}
function json_ask($type = '-1', $msg = '失败', $answer = '0')
{
    return json(['code' => $type, 'msg' => $msg, 'answer' => $answer]);
}
function json_l($j, $count, $msg)
{
    $list["msg"] = $msg;
    $list["code"] = 0;
    $list["count"] = $count;
    $list["data"] = $j;
    return json($list);
}
function json_img($src)
{
    $img['code'] = 0;
    $img['msg'] = '成功';
    $img['src'] = $src;
    return json($img);
}
function seo($title = '', $key = '', $desc = '', $suf = 0, $ask = 0)
{
    $site_seo = \app\admin\model\SiteConfig::get(1);
    $desc = preg_replace("/<br>|\n/", "", $desc);
    $desc = str_replace("</br>", ",", $desc);
    if (!empty($title)) {
        if ($suf == 1) {
            //增加原本后缀 - 检测原内容过长则处理 标题不得超过
            $y_t_c = strlen($site_seo['site_logo_txt']); //原始后缀
            $y_t = strlen($title); //新标题
            $len = 60 - $y_t_c;
            if ($y_t + $y_t_c > 60) {
                $title = mb_substr($title, 0, $len);
            }
            $site_seo['site_title'] = $title . '-' . $site_seo['site_logo_txt'];
        } else {
            $site_seo['site_title'] = $title;
        }
        $title = format_html($title);
    }
    if (!empty($key)) {
        $site_seo['site_keyword'] = $key;
    }
    if (!empty($desc)) {

        if ($ask == 1) {
            //长度不可超出 120
            if ($desc > 120) {
                $desc = mb_substr($desc, 0, 120);
            }
            $site_seo['site_description'] = $title . $desc;
        } else {
            $site_seo['site_description'] = $desc;
        }
        $desc = format_html($desc);
    }
    return $site_seo;
}
function format_html($str)
{
    $str = str_replace('&ldquo;', '“', $str);
    $str = str_replace('&rdquo;', '”', $str);
    $str = str_replace('&middot;', '·', $str);
    $str = str_replace('&lsquo;', '‘', $str);
    $str = str_replace('&rsquo;', '’', $str);
    $str = str_replace('&hellip;', '…', $str);
    $str = str_replace('&mdash;', '—', $str);
    return $str;
}
function get_adimg($url, $img)
{
    $text = "<div class=\"image\"><a href=\"" . $url . "\" target=\"_blank\"><img src=\"" . $img . "\"></a></div>";
    return $text;
}

function p_json($data)
{
    $j['status'] = [
        'code' => 200,
        'message' => '成功'
    ];
    $j['data'] = $data;
    return json($j);
}

function delete_dir_file($dir_name)
{
    $result = false;
    if (is_dir($dir_name)) {
        if ($handle = opendir($dir_name)) {
            while (false !== ($item = readdir($handle))) {
                if ($item != '.' && $item != '..') {
                    if (is_dir($dir_name . DIRECTORY_SEPARATOR . $item)) {
                        delete_dir_file($dir_name . DIRECTORY_SEPARATOR . $item);
                    } else {
                        unlink($dir_name . DIRECTORY_SEPARATOR . $item);
                    }
                }
            }
            closedir($handle);
            if (rmdir($dir_name)) {
                $result = true;
            }
        }
    }
    return $result;
}
